package com.le.tester.javaThink.fifth.chapter18_Str;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * createTime：2021/8/17 9:35
 * description：io流读取文件信息，再使用正则匹配字符
 */
public class JGrep {
    public static void main(String[] args) throws IOException {
        if (args.length < 2) {
            System.out.println("Usage: java JGrep file regex");
            System.exit(0);
        }
        Pattern compile = Pattern.compile(args[1]);
        int index = 0;
        Matcher matcher = compile.matcher("");
        for (String sItem : Files.readAllLines(Paths.get(args[0]))) {
            //重置matcher
            matcher.reset(sItem);
            while (matcher.find()) {
                System.out.println(index++ + ": " + matcher.group() + ": " + matcher.start());
            }
        }
    }
}
